package speechprocess;

import gui.Main;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 *
 * @author Oanh
 */
public class MagnitudePanel extends JPanel {

    Main main;
    double f1;
    double f2;
    double f3;
    double b1;
    double b2;
    double b3;
    boolean ch1;
    boolean ch2;
    boolean ch3;
    public Calculation c;
    double ratio;
    int step;

    public MagnitudePanel(Main main, double f1, double f2, double f3, double b1, double b2, double b3, int step) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
        }
        this.main = main;
        this.f1 = f1;
        this.f2 = f2;
        this.f3 = f3;
        this.b1 = b1;
        this.b2 = b2;
        this.b3 = b3;
        this.step = step;
        ch1 = true;

        c = new Calculation(f1, b1, f2, b2, f3, b3);

        setVisible(true);
        setBounds(150, 20, 1100, 600);
    }

    public void setNewValue(double f1, double f2, double f3, double b1, double b2, double b3, boolean check1, boolean check2, boolean check3, int step) {
        this.f1 = f1;
        this.f2 = f2;
        this.f3 = f3;
        this.b1 = b1;
        this.b2 = b2;
        this.b3 = b3;
        this.step = step;
        ch1 = check1;
        ch2 = check2;
        ch3 = check3;
        c = new Calculation(f1, b1, f2, b2, f3, b3);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 1099, 599);
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, 1099, 599);

        //Khung ngang
        g.drawLine(50, 50, 1050, 50);
        g.drawLine(50, 300, 1070, 300);
        g.drawLine(50, 550, 1050, 550);
        for (int i = 0; i < 1000; i++) {
            if (i % 2 == 0) {
                g.drawLine(50 + i, 175, 50 + i, 175);
                g.drawLine(50 + i, 425, 50 + i, 425);
            }
        }
        //Khung dọc
        g.drawLine(50, 30, 50, 550);
        g.drawLine(1050, 50, 1050, 550);
        for (int i = 0; i < 500; i++) {
            if (i % 2 == 0) {
                g.drawLine(250, 50 + i, 250, 50 + i);
                g.drawLine(450, 50 + i, 450, 50 + i);
                g.drawLine(650, 50 + i, 650, 50 + i);
                g.drawLine(850, 50 + i, 850, 50 + i);
            }
        }
        //Mũi tên
        g.drawLine(45, 35, 50, 30);
        g.drawLine(55, 35, 50, 30);
        g.drawLine(1065, 295, 1070, 300);
        g.drawLine(1065, 305, 1070, 300);
        //
        g.setFont(main.jButton1.getFont());

        g.drawString("-20*log10(H[f])", 60, 35);


        g.drawString("0", 30, 55);
        g.drawString(Math.floor(c.xMax * 50) / 100 + "", 2, 180);
        g.drawString(Math.floor(c.xMax * 50) / 100 + "", 2, 300);
        g.drawString(Math.floor(c.xMax * 150) / 100 + "", 2, 430);
        g.drawString(Math.floor(c.xMax * 200) / 100 + "", 2, 555);
        g.drawString("f", 1075, 290);


        g.drawString("0", 50, 580);
        for (int i = 0; i < 5; i++) {
            g.drawString((i + 1) + "000", (i + 1) * 200 + 35, 580);
        }

        ratio = 250 / c.xMax;
        int f = step;
//        for (f = step; f < 5000; f += step) {
        while(f<5000){
            
            if (ch1) {
                g.setColor(Color.BLUE);
                myDrawLine(g, f - step, c.x1[f - step], f, c.x1[f]);
            }
            if (ch2) {
                g.setColor(Color.GREEN);
                myDrawLine(g, f - step, c.x2[f - step], f, c.x2[f]);
            }
            if (ch3) {
                g.setColor(Color.MAGENTA);
                myDrawLine(g, f - step, c.x3[f - step], f, c.x3[f]);
            }
            if (ch1 && ch2 && ch3) {
                g.setColor(Color.RED);
                myDrawBoldLine(g, f - step, c.HMul[f - step], f, c.HMul[f]);
            }            
            f+=step;
        }
        
        f-=step;
        if (ch1) {
                g.setColor(Color.BLUE);
                myDrawLine(g, f , c.x1[f ],4999, c.x1[4999]);
            }
            if (ch2) {
                g.setColor(Color.GREEN);
                myDrawLine(g, f , c.x2[f ], 4999, c.x2[4999]);
            }
            if (ch3) {
                g.setColor(Color.MAGENTA);
                myDrawLine(g, f , c.x3[f ], 4999, c.x3[4999]);
            }
            if (ch1 && ch2 && ch3) {
                g.setColor(Color.RED);
                myDrawBoldLine(g, f , c.HMul[f ], 4999, c.HMul[4999]);
            }
    }

    public void myDrawLine(Graphics g, double x1, double y1, double x2, double y2) {
        g.drawLine(50 + (int) (x1 / 5), 50 + (int) (y1 * ratio), 50 + (int) (x2 / 5), 50 + (int) (y2 * ratio));
    }

    public void myDrawBoldLine(Graphics g, double x1, double y1, double x2, double y2) {
        g.drawLine(50 + (int) (x1 / 5), 50 + (int) (y1 * ratio), 50 + (int) (x2 / 5), 50 + (int) (y2 * ratio));
        g.drawLine(51 + (int) (x1 / 5), 50 + (int) (y1 * ratio), 51 + (int) (x2 / 5), 50 + (int) (y2 * ratio));
    }

    double min(double x, double y) {
        return min(x, y);
    }

    double max(double x, double y) {
        return max(x, y);
    }
}
